/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipasssystem;

import ua.yandex.skipasssystem.skipass.SkiPass;
import ua.yandex.skipasssystem.skipass.SkiPassFactory;
import ua.yandex.skipasssystem.skipass.SkiPassType;
import ua.yandex.skipasssystem.skipass.AbstractSkiPass;
import ua.yandex.collections.MyArrayList;
import ua.yandex.skipasssystem.skipass.LimitedPass;

/**
 *
 * @author Mitya
 */
public class SkiPassSystem {
    
    private static SkiPassSystem instance;
    private static final int DEFAULT_NUMBER_OF_TURNSTILES = 200;
    private final MyArrayList listOfSuccesfullTransactions;
    private final MyArrayList listOfUnsuccesfullTransactions;
    private final MyArrayList skiPassList;
    private final MyArrayList skiPassTurnstileList;
    private final SkiPassFactory skiPassFactory;
    
    private SkiPassSystem() {
        listOfSuccesfullTransactions = new MyArrayList();
        listOfUnsuccesfullTransactions = new MyArrayList();
        skiPassList = new MyArrayList();
        skiPassTurnstileList = new MyArrayList();
        for (int i = 0; i < DEFAULT_NUMBER_OF_TURNSTILES; i++) {
            skiPassTurnstileList.add((Object) new SkiPassTurnstile(this));
        }
        skiPassFactory = new SkiPassFactory(this);
    }
    
    public static SkiPassSystem getSkiPassSystem() {
        if (instance == null) {
            instance = new SkiPassSystem();
        }
        
        return instance;
    }
    
    public SkiPassFactory getSkiPassFactory() {
        return skiPassFactory;
    }
    
    protected static void reinitializeSystem() {
        instance = new SkiPassSystem();
    }
    
    public void addSkiPass(SkiPass skiPass) {
        skiPassList.add(skiPass);
    }
    
    public SkiPass getSkiPass(int uiid) {
        return (SkiPass) skiPassList.get(uiid);
    }
    
    public boolean checkSkiPass(SkiPass skiPass) {
        
        if (!skiPass.isValid()) {
            listOfUnsuccesfullTransactions.add(skiPass);
            return false;
        } else if (skiPass instanceof LimitedPass) {
            ((LimitedPass) skiPass).decreseNumberOfRides();
        }
        listOfSuccesfullTransactions.add(skiPass);
        return true;
        
    }
    
    public void addTurnstile(int index) {
        skiPassTurnstileList.add(index, new SkiPassTurnstile(this));
    }
    
    public void removeTurnstile(int index) {
        skiPassTurnstileList.remove(index);
    }
    
    public void blockSkiPass(int uiid) {
        ((AbstractSkiPass) skiPassList.get(uiid)).block();
    }
    
    public void unblockSkiPass(int uiid) {
        ((AbstractSkiPass) skiPassList.get(uiid)).unblock();
    }
    
    public int getNumberOfSkiPass() {
        return skiPassList.size();
    }
    
    public int getNumberOfSuccesfullTransactions() {
        return listOfSuccesfullTransactions.size();
    }
    
    public int getNumberOfSuccesfullTransactions(SkiPassType skiPassType) {
        
        int numberOfSucessfullTransactions = 0;
        for (int i = 0; i < listOfSuccesfullTransactions.size(); i++) {
            SkiPass currentSkiPass
                    = (SkiPass) (listOfSuccesfullTransactions.get(i));
            
            if (currentSkiPass.getType().equals(skiPassType)) {
                numberOfSucessfullTransactions++;
            }
        }
        return numberOfSucessfullTransactions;
    }
    
    public int getNumberOfUnsuccesfullTransactions() {
        return listOfUnsuccesfullTransactions.size();
    }
    
    public int getNumberOfUnsuccesfullTransactions(SkiPassType skiPassType) {
        
        int numberOfUnsucessfullTransactions = 0;
        for (int i = 0; i < listOfUnsuccesfullTransactions.size(); i++) {
            SkiPass currentSkiPass
                    = (SkiPass) (listOfUnsuccesfullTransactions.get(i));
            
            if (currentSkiPass.getType().equals(skiPassType)) {
                numberOfUnsucessfullTransactions++;
            }
        }
        return numberOfUnsucessfullTransactions;
        
    }
    
}
